package com.sea.finance.controller;

import com.codecrane.core.power.service.DictDetailService;
import com.codecrane.core.util.DateUtils;
import com.codecrane.core.util.ExcelUtil;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sea.finance.entity.ExchangeRate;
import com.sea.finance.entity.ExchangeRateRecord;
import com.sea.finance.service.ExchangeRateRecordService;
import com.sea.finance.service.ExchangeRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;


/**
 *  控制器
 * @author 陈鸿飞
 *  <br/>
 *  <br/>
 */
@Controller
@RequestMapping("/sea/finance/rate")
public class ExchangeRateController {


    @Autowired
    private ExchangeRateService exchangeRateService;
    @Autowired
    private DictDetailService dictDetailService;
    @Autowired
    private ExchangeRateRecordService exchangeRateRecordService;

    /**
     * 管理主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        return "/sea/finance/rate/main";
    }

    /**
     * 管理主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/record/main")
    public String recordMain(Model model,@RequestParam("rateId")Integer rateId) {
        model.addAttribute("rateId",rateId);
        return "/sea/finance/rate/record";
    }

    @ResponseBody
    @RequestMapping("/findByRateId")
    public AjaxTableReturn findByRateId(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     Integer rateId) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<ExchangeRateRecord> list = exchangeRateRecordService.findByRateId(rateId);
        PageInfo<ExchangeRateRecord> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 新增主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/addPage")
    public String addPage(Model model) {
        //国家
        model.addAttribute("country", dictDetailService.findByTypeCode("base_country", null));
        return "/sea/finance/rate/add";
    }

    /**
     * 新增主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/editPage")
    public String editPage(Model model,@RequestParam("id")Integer id) {
        //国家
        model.addAttribute("country", dictDetailService.findByTypeCode("base_country", null));

        model.addAttribute("rate", exchangeRateService.findById(id));
        return "/sea/finance/rate/edit";
    }


    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     ExchangeRate rate) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<ExchangeRate> list = exchangeRateService.findByCnd(rate);
        PageInfo<ExchangeRate> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 新增
     * @param exchangeRate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxReturn save(ExchangeRate exchangeRate) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try{
            if (null != exchangeRate && exchangeRateService.save(exchangeRate) > 0) {
                result.setOk(true).setMsg("保存成功！");
            }
        }catch (DuplicateKeyException e){
            result.setMsg("当前汇率已重复,请勿重复添加！");
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }

    /**
     * 新增
     * @param exchangeRate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(ExchangeRate exchangeRate) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try{
            if (null != exchangeRate && exchangeRateService.usave(exchangeRate) > 0) {
                result.setOk(true).setMsg("保存成功！");
            }
        }catch (DuplicateKeyException e){
            result.setMsg("当前汇率已重复,请勿重复添加！");
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
            return result;
        }
        return result;
    }
}
